package com.naiterui.ehp.bs.ds.modules.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.ds.common.controller.AbstractController;
import com.naiterui.ehp.bs.ds.modules.sys.entity.Dept;
import com.naiterui.ehp.bs.ds.modules.sys.service.IDeptService;

import io.swagger.annotations.Api;

/**
 * 部门管理
 */
@Validated
@Api(tags = {SwaggerTag.DS + SwaggerTag.SYS})
@RestController
@RequestMapping("/sys/dept")
public class DeptController extends AbstractController {

    @Autowired
    private IDeptService deptService;

    /**
     * 列表
     *
     * @return
     */
    @SysLog("部门列表")
    @GetMapping("/list")
    @PreAuthorize("@p.check('sys:dept:list')")
    public List<Dept> list() {
        return this.deptService.queryList();
    }

    /**
     * 选择部门(添加、修改菜单)
     *
     * @return
     */
    @SysLog("选择部门(添加、修改菜单)")
    @GetMapping("/select")
    @PreAuthorize("@p.check('sys:dept:select')")
    public List<Dept> select() {
        return this.deptService.select();
    }

    /**
     * 信息
     *
     * @return
     */
    @SysLog("部门信息")
    @GetMapping("/info/{deptId}")
    @PreAuthorize("@p.check('sys:dept:info')")
    public Dept info(@PathVariable("deptId") Long deptId) {
        return this.deptService.getById(deptId);
    }

    /**
     * 保存
     */
    @SysLog("保存部门")
    @PostMapping("/save")
    @PreAuthorize("@p.check('sys:dept:save')")
    public void save(@RequestBody Dept dept) {
        this.deptService.save(dept);
    }

    /**
     * 修改
     */
    @SysLog("修改部门")
    @PostMapping("/update")
    @PreAuthorize("@p.check('sys:dept:update')")
    public void update(@RequestBody Dept dept) {
        this.deptService.updateById(dept);
    }

    /**
     * 删除
     */
    @SysLog("删除部门")
    @GetMapping("/delete/{deptId}")
    @PreAuthorize("@p.check('sys:dept:delete')")
    public void delete(@PathVariable("deptId") long deptId) throws BusinessException {
        this.deptService.delete(deptId);
    }

}
